package dianmobile.eistarandroid.ui.views;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.beans.Item;
import dianmobile.eistarandroid.beans.PostsIntroduction;
import dianmobile.eistarandroid.ui.Adapter.BoardContentListAdapter;
import dianmobile.eistarandroid.ui.activities.ViewPostActivity;
import dianmobile.eistarandroid.utils.ConstantsData;
import zrc.widget.SimpleHeader;
import zrc.widget.ZrcListView;

public class BoardContentListView extends LinearLayout{

    private int listType = 0;
    private ZrcListView postsLv;
    private PostLvItemClickListener listViewItemLsn;
    private Context context;
    private BoardContentListAdapter boardContentListAdapter;
    private List<Object> listData = new ArrayList<>();

    public BoardContentListView(Context context, int listType) {
        super(context);
        this.context = context;
        this.listType = listType;
        initList(context);
    }

    public BoardContentListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initList(context);
    }

    private boolean isInitedList = false;
    private void initList(Context context) {
        if (isInitedList)
            return ;

        isInitedList = true;

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.view_board_content_list, this);

        postsLv = (ZrcListView) view.findViewById(R.id.lv_posts);
        setPullToRefresh();

        boardContentListAdapter = new BoardContentListAdapter(context, listData, listType);
        postsLv.setAdapter(boardContentListAdapter);
        listViewItemLsn = new PostLvItemClickListener(context, listData, listType);
        postsLv.setOnItemClickListener(listViewItemLsn);
        getListData();
    }

    private void getListData(){

    }

    /**
     * 更新列表的数据
     * @param data
     */
    private void listDataChanged(List<Object> data) {
        boardContentListAdapter.setValueList(data);
        boardContentListAdapter.notifyDataSetChanged();
        listViewItemLsn.setListData(data);
        if (isPullToRefreshing) {
            postsLv.setRefreshSuccess();
            isPullToRefreshing = false;
        }
    }

    /**
     * 如果为true,表示列表正在下拉刷新;反之亦然.
     */
    private boolean isPullToRefreshing = false;

    private void setPullToRefresh(){
        SimpleHeader header = new SimpleHeader(context);
        header.setTextColor(0xff0066aa);
        header.setCircleColor(0xff33bbee);
        postsLv.setHeadable(header);

        postsLv.setOnRefreshStartListener(new ZrcListView.OnStartListener() {
            @Override
            public void onStart() {
                isPullToRefreshing = true;

                setRequestNetworkFailedReceiver();
            }
        });

    }

    private BroadcastReceiver requestNetworkFailedReceiver = null;

    private void setRequestNetworkFailedReceiver() {
        if (null == requestNetworkFailedReceiver) {
            requestNetworkFailedReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (isPullToRefreshing) {
                        postsLv.setRefreshFail("网络请求失败");
                        isPullToRefreshing = false;
                    }
                }
            };

            // 注册广播
            LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(getContext());
            IntentFilter filter = new IntentFilter();
            filter.addAction(ConstantsData.ACTION_REQUEST_NETWORK_FAILED);
            localBroadcastManager.registerReceiver(requestNetworkFailedReceiver, filter);

            //TODO 注销广播
            // 注销广播应该在onDestroyed函数执行合适,但是没有想到我们的代码都放在了LinearLayout类里面
            // 这就意味着该类里面没有关于Activity生命周期的回调
            // 看来要重构了 = =
        }
    }

}

class PostLvItemClickListener implements ZrcListView.OnItemClickListener {

    private Context context;
    private List<Object> list;
    private int listType;
    PostLvItemClickListener(Context context, List<Object> list, int listType){
        this.context = context;
        this.list = list;
        this.listType = listType;
    }
    @Override
    public void onItemClick(ZrcListView parent, View view, int position, long id) {
        Intent intent = new Intent(context, ViewPostActivity.class);
        switch (listType) {
            case ConstantsData.ENUM_TOP10_LIST_VIEW:
                PostsIntroduction postsIntroduction = (PostsIntroduction)list.get(position);
                intent.putExtra(ConstantsData.EXTRA_FILE_NAME, postsIntroduction.getFile());
                intent.putExtra(ConstantsData.EXTRA_BOARD_NAME, postsIntroduction.getBoard());
                break;
            case ConstantsData.ENUM_RECOMMENDATION_LIST_VIEW:
                Item item = (Item)list.get(position);
                intent.putExtra(ConstantsData.EXTRA_FILE_NAME, item.getFilename());
                intent.putExtra(ConstantsData.EXTRA_BOARD_NAME, item.getBoard());
                break;
            default:
                break;
        }
        context.startActivity(intent);
    }

    public void setListData(List<Object> listData) {
        this.list = listData;
    }
}